﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using System.Data;
using System.Data.SqlClient;
namespace RED_NET.MasterPage
{
    public partial class Content : System.Web.UI.MasterPage
    {
        private static Users user;
        protected static int  visitId;
        protected static string visitNickName;
        protected void Init()
        {
            user = new Users();
            user.UserId = -1;
            if (Session["USER"] != null)
                user = (Users)Session["USER"];
            
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");

            LoginBLL _login = new LoginBLL();
            if (user.UserId!= -1)
            {
                if (Session["PreVisit"] == null || (int)Session["PreVisit"] != visitId)
                {
                    Session["PreVisit"] = visitId;
                    if (visitId != user.UserId && user.UserId != -1)
                    {
                        try
                        {
                            _login.AddVisitInfo(user.UserId, visitId);
                        }
                        catch (Exception ex)
                        {
                            Response.Redirect("~/ContentPage/Error/PageError.aspx");
                        }


                    }
                }
            }

            Users _user = new Users();
            try
            {
                _user = _login.GetMessageOfUser(visitId);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }

            if (visitId != user.UserId)
            {
                p_Latested.Visible = false;
                p_Letter.Visible = false;
                p_MyAttention.Visible = false;
                p_PublicDaily.Visible = false;
                p_UpdateInfo.Visible = false;
               
                    p_GoToMyMainPage.Visible = true;
              
                   
                p_Admin.Visible = false;
                SendLetter.Visible = true;
            }
            else
            {
              
               
                p_GoToMyMainPage.Visible = false;
                p_Latested.Visible = true;
                p_Letter.Visible = true;
                p_MyAttention.Visible = true;
                p_UpdateInfo.Visible = true;
                p_PublicDaily.Visible = true;
                SendLetter.Visible = false;
                LinkButton_Letter.Text = "站内信:" + News_BLL.GetLetterNum(user.UserId).ToString() + "";
            }

         

            if (user.UserId == visitId || user.UserId == -1 || _login.JudgeUserRel(user.UserId, visitId) == 1)
            {
                Add_Attention.Visible = false;
            }
            else
                Add_Attention.Visible = true;
            if (user.UserId == -1)
            {
                Add_Attention.Visible = false;
                LinkButton_GoToMyMainPage.Visible = false;
                p_GoToMyMainPage.Visible = false;
                p_UpdateInfo.Visible = false;
                p_PublicDaily.Visible = false;
                p_MyAttention.Visible = false;
                p_Letter.Visible = false;
                p_Latested.Visible = false;
                p_Admin.Visible = false;
                SendLetter.Visible = false;
            }
            Image_UserAvatar.ImageUrl = _user.AvatarUrl;
            Label_NickName.Text = _user.NickName;
            visitNickName = _user.NickName;
            DataTable _dt = _login.GetLatestVisitor(visitId);
            DataList_LatestVisit.DataSource = _dt;
            DataList_LatestVisit.DataKeyField = "_VisitId";
            DataList_LatestVisit.DataBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {
           
                //检测用户是否登录
            Init();
            //母版页登录
          
            if (Session["PageName"] == null)
            {
                ContentDaohangCss.Href = "../Image_Css hang_Css/ContenMasterCssdaohang_blue.css";
            }
            else
            {
                string pageName = Session["PageName"].ToString();
              if (pageName == "Album")
                {
                    ContentDaohangCss.Href = "../Image_Css hang_Css/ContenMasterCssdaohang_fenhong.css";
                }
                else if (pageName == "Manager")
                {
                    ContentDaohangCss.Href = "../Image_Css hang_Css/ContenMasterCssdaohang_blue.css";
                }
                else if (pageName == "NewsFeed")
                {
                    ContentDaohangCss.Href = "../Image_Css hang_Css/ContenMasterCssdaohang_chenghuang.css";
                }
                else ContentDaohangCss.Href = "../Image_Css hang_Css/ContenMasterCssdaohang_blue.css";
            }

                if (Session["USER"] != null)//登录好了
                {
                    Users user = (Users)Session["USER"];
                    Label1.Text = "欢迎你，" + user.NickName + "!";
                    LinkButton2.Visible = LinkButton3.Visible = true;
                    PreLogin.Visible = false;
                }
                else//未登录
                {
                    Label1.Text = "宁大师生欢迎你！";
                    LinkButton2.Visible = LinkButton3.Visible = false;
                    PreLogin.Visible = true;
                }
        }
        protected void LinkButton_Main_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/User/MainPage.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_Daily_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/News/DailyList.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_PublicDaily_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/News/PublicDaily.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_Album_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/Album.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_Letter_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/News/SendLetter.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_MessageBoard_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/News/MessageBoard.aspx?VISIT_ID=" + visitId.ToString());
        }
        protected void LinkButton_MyAttention_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/User/MyAttentionList.aspx?VISIT_ID=" + visitId.ToString());
        }

        protected void LinkButton_GoToMyMainPage_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/User/MainPage.aspx?VISIT_ID=" + user.UserId.ToString());
        }

        protected void LinkButton_Admin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Manager's/Manager.aspx?VISIT_ID=" + user.UserId.ToString());
        }
        protected void LinkButton_UpdateInfo_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/User/UpdateInfo.aspx?VISIT_ID=" + user.UserId.ToString());
          
           
        }
        protected void LinkButton_Latested_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/NewsFeeds/NewsFeed_News.aspx?VISIT_ID=" + user.UserId.ToString());
        }
        protected void LinkButton_JoinAttention_Click(object sender, EventArgs e)
        {
            LoginBLL _login = new LoginBLL();
            try{
                _login.AddAttention(user.UserId, visitId);
            }
            catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
            LinkButton_JoinAttention.Visible = false;
        }
        protected void LinkButton_SendLetter_Click(object sender, EventArgs e)
        {
            Session["SendTo_Id"] = visitId;
            Session["SendTo_NickName"] = visitNickName;
            Response.Redirect("~/ContentPage/News/SendLetter.aspx?VISIT_ID=" + user.UserId.ToString());
        }

      
        public void PopulateNode(Object sender, TreeNodeEventArgs e)
        {
           
                LoginBLL _login = new LoginBLL();
                DataTable _dt = _login.GetCollegeUser();

                foreach (DataRow _dr in _dt.Rows)
                {
                    TreeNode NewNode = new TreeNode(_dr["_NickName"].ToString());
                    DataTable _dt2 = new DataTable();
                    try{
                    _dt2 = _login.GetClassUser(Int32.Parse(_dr["_College"].ToString()));
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("~/ContentPage/Error/PageError.aspx");
                    }


                    // Set additional properties for the node.
                    NewNode.SelectAction = TreeNodeSelectAction.Expand;
                    TreeNode tNewNode = new TreeNode("学院主页");
                    tNewNode.NavigateUrl = "~/ContentPage/User/MainPage.aspx?VISIT_ID=" + _dr["_UId"].ToString();
                    tNewNode.SelectAction = TreeNodeSelectAction.Select;
                    NewNode.ChildNodes.Add(tNewNode);
                    foreach (DataRow _dr2 in _dt2.Rows)
                    {

                        NewNode.PopulateOnDemand = true;
                        TreeNode _newNode2 = new TreeNode(_dr2["_NickName"].ToString());
                        _newNode2.PopulateOnDemand = false;
                        _newNode2.SelectAction = TreeNodeSelectAction.Select;
                        _newNode2.NavigateUrl = "~/ContentPage/User/MainPage.aspx?VISIT_ID=" + _dr2["_UId"].ToString();
                        NewNode.ChildNodes.Add(_newNode2);

                    }
                    // Add the new node to the ChildNodes collection of the parent node.
                    e.Node.ChildNodes.Add(NewNode);

                }
                e.Node.CollapseAll();
            
        }
        protected void TreeView_Navigetion_Selected(object sender, EventArgs e)
        {

        }

        protected void PreLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ContentPage/Login_2.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
           // Session["USER"] = null;
          
            //放这是为了在PageLoad后面执行
        /*    if (Session["USER"] != null)//登录好了
            {
                Users user = (Users)Session["USER"];
                uId = user.UserId;
                Label1.Text = "欢迎你，" + user.NickName + "!";
                LinkButton2.Visible = LinkButton3.Visible = true;
                PreLogin.Visible = false;
            }
            else//未登录
            {*/ 
            Session.Remove("USER");
                Label1.Text = "宁大师生欢迎你！";
                LinkButton2.Visible = LinkButton3.Visible = false;
                PreLogin.Visible = true;
                Add_Attention.Visible = false;
                LinkButton_GoToMyMainPage.Visible = false;
                p_GoToMyMainPage.Visible = false;
                p_UpdateInfo.Visible = false;
                p_PublicDaily.Visible = false;
                p_MyAttention.Visible = false;
                p_Letter.Visible = false;
                p_Latested.Visible = false;
                p_Admin.Visible = false;
                SendLetter.Visible = false;
           // }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Users user = (Users)Session["USER"];
            Response.Redirect("~/ContentPage/User/MainPage.aspx?VISIT_ID=" + user.UserId.ToString());
        }

  



    }
}
